package com.moduleSys.resources;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.slf4j.MDC;

/**
 * ip 用于日志中输出
 */
@WebFilter( filterName = "mdcFilter", urlPatterns = { "/*" } )
public class MDCFilter implements Filter {

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter( final ServletRequest request, final ServletResponse response, final FilterChain chain )
            throws IOException, ServletException {
        final String parameter = request.getRemoteAddr();
        MDC.put( "ip", parameter );
        try {
            chain.doFilter( request, response );
        } finally {
            MDC.remove( "ip" );
        }
    }

    @Override
    public void init( final FilterConfig filterConfig ) throws ServletException {

    }
}